package com.example.demo.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.R;
import com.example.demo.dto.FoodDto;
import com.example.demo.entity.Food;
import com.example.demo.entity.Trace;
import com.example.demo.entity.User;
import com.example.demo.entity.Vitamin;
import com.example.demo.mapper.FoodMapper;
import com.example.demo.mapper.FoodtvrelationMapper;
import com.example.demo.service.FoodService;
import com.example.demo.service.FoodtvrelationService;
import com.example.demo.service.TraceService;
import com.example.demo.service.VitaminService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/food")
@Slf4j
@CrossOrigin(origins = "*", maxAge = 3600)
public class FoodController {

    @Autowired
    private FoodtvrelationService foodtvrelationService;

    @Autowired
    @Lazy
    private FoodService foodService;

    @Autowired
    private FoodMapper foodMapper;


    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping
    @PreAuthorize("hasAuthority('admin')") //用户权限
    public R<String>  delete(Long id){
        foodtvrelationService.deleteFoodTVRelation(id);
        return R.success("删除成功");
    }

    /**
     * 查到所有
     * @return
     */
    @GetMapping("/all")
    public R<List<Food>> getAll(){
        List<Food> foods = foodMapper.selectList(null);
        return R.success(foods);
    }

    /**
     * 食物分页查询
     *
     * @param page
     * @param pageSize
     * @param foodName
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String foodName) {
        log.info("page = {},pageSize = {},foodName = {}", page, pageSize, foodName);
        Page<Food> allFood = foodtvrelationService.getAllFood(page, pageSize, foodName);

        return R.success(allFood);
    }

    /**
     * 根据ID查询食物信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<FoodDto> getById(@PathVariable Long id) {
        log.info("根据id查询食物信息...");

        FoodDto byIdWithVitaminTrace = foodtvrelationService.getByIdFood(id);

        return R.success(byIdWithVitaminTrace);
    }


    /**
     * 新增食物
     *
     * @return
     */
    @PostMapping("/add")
    @PreAuthorize("hasAuthority('admin')") //用户权限
    public R<String> save(@RequestBody FoodDto foodDto) {
        log.info("新增食物，食物信息：{}", foodDto.toString());
        LambdaQueryWrapper<Food> foodLambdaQueryWrapper = new LambdaQueryWrapper<>();
        foodLambdaQueryWrapper.eq(Food::getFoodName, foodDto.getFoodName());
        List<Food> list = foodService.list(foodLambdaQueryWrapper);
        if (list.size() != 0 ) {
            return R.error("食物已存在");
        } else {
            foodtvrelationService.saveFood(foodDto);
            return R.success("新增食物成功");
        }

    }

    /**
     * 更新食物
     *
     * @return
     */
    @PatchMapping
    @Transactional
    @PreAuthorize("hasAuthority('admin')") //用户权限
    public R<String> update(@RequestBody FoodDto foodDto) {
        log.info("修改食物，食物信息：{}", foodDto.toString());

        // if (foodDto.getIdDelete() !=1){
        //     R.error("此食物已废弃");
        // }
        foodtvrelationService.updateFood(foodDto);

        return R.success("修改食物成功");
    }

}
